var userRoleList = function(targetObj){
	this.sysUtils = new kendoUtils();
	this.targetObj = targetObj;
	this.init();
	return this;
};

userRoleList.prototype={
		init:function(){
			var bindFieldArray = this.sysUtils.getBindFieldArray(this.targetObj.find(".search-panel"));
			var bindFunctionArray = [{fName:"queryInfo",fs:this.findGridData},{fName:"resetInfo",fs:this.resetInfo}];
			this.bindBaseModels = new bindBaseModel(null,bindFieldArray,bindFunctionArray,null);
			this.sysUtils.bindDoucment(this.bindBaseModels,this.targetObj,this);
			this.createGrid();
		},
		/*
		 * 创建空列表
		 */
		createGrid:function(){
			var columns = this.gridColumns();
			var that = this;
			this.sysUtils.createGird(this.targetObj.find("#userRoleList"), {
				dataSource:[],
				selectable: "single",
				reorderable: true,
				columnMenu: true,
				resizable: true,
				pageable: {input: true,
					numeric: false},
					columns: columns,
					dataBound:function(){
						that.targetObj.find("#userRoleList").find("tr:gt(0)").unbind("dblclick");
						that.targetObj.find("#userRoleList").find("tr:gt(0)").dblclick(function(e){
							that.openwindow(that.selectRow(),false);
						});	
					}
			});
		},
		/**
		 * 选中行
		 */
		selectRow:function(){
			var grid = this.targetObj.find("#userRoleList").data("kendoGrid");
			var dataItem = grid.dataItem(grid.select());
			if(isEmpty(dataItem)){
				return null;
			}
			var id = dataItem["id"];
			return id;
		},
		/**
		 * 打开用户角色信息窗口
		 */
		openwindow:function(id,isEdit){
			//打开查看窗口
			var title = "添加角色信息";
			this.editResult = null;
			var that = this;
			var opens = function(){
				that.isEdit = isEdit;
				that.openDivId = that.sysUtils.openWindow(that.openDivId,{
					title:title,
					height:"500px",
					width:"650px",
					resizable:false,
					content:"views/framework/userRole/userRole_Edit.html"
				}, true);
			};
			var loadData = function(){
				JsonpAjax(baseApiPath+"userRole/queryById?userRoleId="+id,null,"json",function(result){
					that.editResult = result;
					opens();
				}); 
			}; 
			if(isNotEmpt(id) && false == isEdit){
				title = "查看角色信息";
				loadData();
			}else if(isEmpty(id) && true == isEdit){
				title = "编辑角色信息";
				id = that.selectRow();
				if(null == id){
					notification({ title:"提示信息",message:"请选择需要编辑的角色信息!！"},'info');
					return;
				}
				loadData();
			}else{
				opens();
			}
		},
		/*
		 * 重置按钮
		 */
		resetInfo:function(e){
			var that = $(e.target).data("that");
			that.sysUtils.setBindFieldNull(that.bindBaseModels.bindFieldArray);
		},
		findGridData:function(e){
			var that = this;
			if(isNotEmpt(e)){
				that = $(e.target).data("that");
			}
			if(validityForm(that.targetObj)){
				var jsonObj = that.sysUtils.getBindDocJson();
				var kendoGrid  = that.targetObj.find("#userRoleList").data("kendoGrid");
				var urls = baseApiPath +"userRole/pagingList";
				var dataT = {curdatetime:new Date().getTime(),json:JSON.stringify(jsonObj)};
				var dataSource = that.sysUtils.getDataSource({url:urls,data:dataT,dataType:"odata"});
				kendoGrid.setDataSource(dataSource); 
				that.targetObj.find("input[gridid]")[0].checked = false;
			}
		},
		/*
		 * 加载list 列表
		 */
		gridColumns:function(){
			return [{
				width:30,
				headerTemplate: '<input type="checkbox" gridId="userRoleList" onclick="gridCheckAll(this)" />',
				template: '<input name="childCheck" type="checkbox" value="${id}"/>'
			},{
				title: "角色名称",
				field: "roleName"
			},
			{
				title: "角色编号",
				field:"roleCode"
			},
			{
				title: "创建人",
				field: "createBy"
			},
			{
				title: "创建时间",
				field: "createTime"
			},
			{
				title: "最后更新人",
				field:"lastUpdateBy"
			},
			{
				title: "最后更新时间",
				field:"lastUpdateTime"
			}];
		},
		/**
		 * 删除用户角色信息
		 */
		delUserRole:function(){
			//获取呗选中的选择框
			var that = this;
			var valStr = "";
			that.targetObj.find("input[name='childCheck']:checked").each(function(){
				valStr += "'" + $(this).val() +"',";
			});
			if("" == valStr){
				notification({ title:"操作失败",message:"请勾选至少一条需要删除的记录!!"},'info');
				return;
			}
			notification({ title:"确认信息",message:"确定需要删除选中的用户角色信息？"},'confirm',{callback:function(){
				valStr = valStr.substring(0, valStr.length - 1);
				JsonpAjax(baseApiPath+"userRole/delUserRoleById?userRoleIds="+valStr,"","json",function(result){
					that.findGridData();
				});
			}});
		}
};



var userRoleEdit = function(targetObj){
	this.sysUtils = new kendoUtils();
	this.targetObj = targetObj;
	this.initFlag = true;
	this.init();
	return this;
};

userRoleEdit.prototype={
		init:function(){
			var bindFieldArray = this.sysUtils.getBindFieldArray(this.targetObj);
			var bindFunctionArray = [{fName:"saveUserRoleInfo",fs:this.save},{fName:"closeWindow",fs:closeWindow}];
			this.bindBaseModels = new bindBaseModel(null,bindFieldArray,bindFunctionArray,null);
			this.sysUtils.bindDoucment(this.bindBaseModels,this.targetObj,this);
			this.initResourceTree();
			this.editFullData();
		},
		/**
		 * 初始化权限资源树(超级用户登录显示所有可用，对应用户登录，显示自身最大权限)
		 */
		initResourceTree:function(){
			var that = this;
			JsonpAjax(baseApiPath + "userRole/dataUserRoleTree",null,"json",function(result){
				if(result.constructor == Object){
					that.targetObj.find("#userRoleTree").kendoTreeView({
						dataTextField:"fullName",
						dataSource:[result],
						checkboxes: {
							checkChildren: false,
							template: "<input type='checkbox' id='#= item.id #' />"
						},
						schema: {
							model: {
								hasChildren: "hasChildren",
								id: "id",
								children: "items",
								expanded:"expanded"
							}
						},
						dataBound:function(e){
							if(undefined == e["node"]){   //判断是否节点是否加载完成
								//默认选中根节点
								if (null != userRoleJs.editResult && that.initFlag){
									var resourceList =  userRoleJs.editResult["roleResourceList"];
									if(null != resourceList && resourceList.length > 0){
										for(var i = 0; i < resourceList.length;i ++){
											var checked = that.targetObj.find("#"+resourceList[i]["resourceId"]);
											checked[0].checked = true;
										}
									}
									that.initFlag = false;
								}else if(null != userRoleJs.editResult && false == userRoleJs.isEdit){
									that.targetObj.find("input[type='checkbox']").attr("disabled","disabled");
								}
							}
						}
					});
				}
			});
		},
		editFullData:function(){
			if(null != userRoleJs.editResult){
				this.sysUtils.setBindFieldValue(userRoleJs.editResult);
				//系统编号不允许随意更改
				this.targetObj.find("input[data-bind='value:systenCode']").attr("disabled","disabled");
				if(false == userRoleJs.isEdit){ //查看模式
					this.targetObj.find("input").attr("disabled","disabled");
					this.targetObj.find("textarea").attr("disabled","disabled");
					this.targetObj.find(".save").remove();
				}
			}else{
				var roleCode = "RS" + new Date().pattern("yyyyMMdd") + Utils.complementDigit(Math.floor(Math.random()*1000),4);
				this.sysUtils.setBindFieldValue({"roleCode":roleCode});
			}
		},
		//保存用户角色信息
		save:function(e){
			var that =  $(e.target).data("that");
			if(validityForm(that.targetObj)){
				var systemUserRole = that.sysUtils.getBindDocJson();
				//获取权限组织
				var treeView = that.targetObj.find("#userRoleTree").data("kendoTreeView");
				var nodes = treeView.dataSource.view();
				var nodeList = new Array();
				getCheckedNode(treeView, nodes, nodeList);
				if(nodeList.length <=0){
					notification({ title:"提示信息",message:"请选择至少一个系统权限信息!！"},'info');
					return;
				}
				var resourceList = new Array();
				for(var i = 0;i < nodeList.length ; i++){
					var modelJson = nodeList[i]["model"];
					var resourceJson = {
							resourceType:modelJson["resourceType"], //资源类型
							id:modelJson["id"], //资源ID
							resourceCode:modelJson["resourceCode"], //资源编码
					}
					resourceList.push(resourceJson);
				}
				systemUserRole["resourceList"] = resourceList;
				var url = baseApiPath+"userRole/save";
				if(true == userRoleJs.isEdit){
					url = baseApiPath+"userRole/update";
					systemUserRole["id"] = userRoleJs.editResult["id"];
					systemUserRole["createBy"] = userRoleJs.editResult["createBy"];
					systemUserRole["createTime"] = userRoleJs.editResult["createTime"];
				}
				JsonpAjax(url,JSON.stringify(systemUserRole),"json",function(result){
					closeWindow(); //关闭窗口
					userRoleJs.findGridData();
				}); 
			}
		}
};